package com.bytedance.common.plugin.component;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentProvider;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Window;

import com.bytedance.common.plugin.framework.PluginManager;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.reflect.Reflect;

public class ComponentHelper {
    public enum COMPONENT_TYPE {
        ACTIVITY,
        SERVICE,
        PROVIDER,
        RECEIVER
    }


    public ComponentHelper() {

    }

    public static void attach(Service arg5, Service arg6, Reflect arg7, Reflect arg8) {
        if (arg5 != null && arg6 != null && arg7 != null && arg8 != null) {
            arg8.call("attachBaseContext", new Class[]{Context.class}, new Object[]{arg5});
            arg8.set("mThread", arg7.getFieldValue("mThread", new Class[0]));
            arg8.set("mClassName", arg7.getFieldValue("mClassName", new Class[0]));
            arg8.set("mToken", arg7.getFieldValue("mToken", new Class[0]));
            arg8.set("mApplication", arg5.getApplication());
            arg8.set("mActivityManager", arg7.getFieldValue("mActivityManager", new Class[0]));
            arg8.set("mStartCompatibility", arg7.getFieldValue("mStartCompatibility", new Class[0]));
        }
    }

    @TargetApi(value = 11)
    public static void attach(Activity arg12, Activity arg13, Reflect arg14, Reflect arg15) {
        int v11 = 11;
        if (arg12 != null && arg13 != null && arg14 != null && arg15 != null) {
            arg15.call("attachBaseContext", new Class[]{Context.class}, new Object[]{arg12});
            if (Build.VERSION.SDK_INT >= v11) {
                try {
                    Object v0_1 = arg15.getFieldValue("mFragments", new Class[]{Class.forName("android.app.FragmentManagerImpl")});
                    if (v0_1 == null) {
//                        goto label_58;
                    } else {
                        Reflect.on(v0_1).call("attachActivity", new Class[]{Activity.class, Class.forName("android.app.FragmentContainer"), Fragment.class},
                                new Object[]{arg13, arg15.getFieldValue("mContainer", new Class[]{Class.forName("android.app.FragmentContainer")}), null});
                    }
                } catch (Throwable v0) {
                    v0.printStackTrace();
                }
            }

//            label_58:
            Window v0_2 = arg12.getWindow();
            v0_2.setCallback(arg13);
            LayoutInflater v1 = v0_2.getLayoutInflater();
            if (v1 != null) {
                Reflect v1_1 = Reflect.on(v1);
                try {
                    v1_1.call("setPrivateFactory", new Class[]{LayoutInflater.Factory2.class}, new Object[]{arg13});
                } catch (Throwable v1_2) {
                }
            }

            arg15.set("mWindow", v0_2);
            arg15.set("mUiThread", Thread.currentThread());
            arg15.set("mMainThread", arg14.getFieldValue("mMainThread", new Class[0]));
            PluginManager.getPluginManager();
            arg15.getFieldValue("mInstrumentation", null);
            arg15.set("mToken", arg14.getFieldValue("mToken", new Class[0]));
            arg15.set("mIdent", arg14.getFieldValue("mIdent", new Class[0]));
            arg15.set("mApplication", arg12.getApplication());
            arg15.set("mIntent", arg12.getIntent());
            arg15.set("mComponent", arg12.getIntent().getComponent());
            arg15.set("mActivityInfo", arg14.getFieldValue("mActivityInfo", new Class[0]));
            arg13.setTitle(arg12.getTitle());
            arg15.set("mParent", arg14.getFieldValue("mParent", new Class[0]));
            arg15.set("mEmbeddedID", arg14.getFieldValue("mEmbeddedID", new Class[0]));
            if (Build.VERSION.SDK_INT >= v11) {
                arg15.set("mLastNonConfigurationInstances", arg14.getFieldValue("mLastNonConfigurationInstances", new Class[0]));
            } else {
                arg15.set("mLastNonConfigurationInstance", arg14.getFieldValue("mLastNonConfigurationInstance", new Class[0]));
                arg15.set("mLastNonConfigurationChildInstances", arg14.getFieldValue("mLastNonConfigurationChildInstances", new Class[0]));
            }

            arg15.set("mWindowManager", v0_2.getWindowManager());
            arg15.set("mCurrentConfig", arg14.getFieldValue("mCurrentConfig", new Class[0]));
            Reflect.on(arg12.getBaseContext()).call("setOuterContext", new Class[]{Context.class}, new Object[]{arg13});
        }
    }

    private static Object loadClass(ClassLoader arg2, String arg3) {
        Object v0 = null;
        if (arg2 != null && !TextUtils.isEmpty(arg3)) {
            try {
                v0 = arg2.loadClass(arg3).newInstance();
            } catch (Throwable v1) {
            }
        }

        return v0;
    }

    public static Activity loadPluginActivity(ClassLoader arg2, String arg3) {
        Object v0 = ComponentHelper.loadClass(arg2, arg3);
        if (!(v0 instanceof Activity)) {
            return null;
        }

        return ((Activity) v0);
    }

    public static ContentProvider loadPluginProvider(ClassLoader arg2, String arg3) {
        Object v0 = ComponentHelper.loadClass(arg2, arg3);
        if (!(v0 instanceof ContentProvider)) {
            return null;
        }

        return ((ContentProvider) v0);
    }

    public static BroadcastReceiver loadPluginReceiver(ClassLoader arg2, String arg3) {
        Object v0 = ComponentHelper.loadClass(arg2, arg3);
        if (!(v0 instanceof BroadcastReceiver)) {
            return null;
        }

        return ((BroadcastReceiver) v0);
    }

    public static Service loadPluginService(ClassLoader arg2, String arg3) {
        Object v0 = ComponentHelper.loadClass(arg2, arg3);
        if (!(v0 instanceof Service)) {
            return null;
        }

        return (Service) v0;
    }

    public static Intent[] tryConvertPlugin2Proxy(Context arg9, COMPONENT_TYPE arg10, Intent[] arg11) {
        int v0_2;
        ResolveInfo v2_2;
        String v2_1;
        int v1;
        int v3;
        if (arg9 != null && arg11 != null) {
            try {
                int v4 = arg11.length;
                v3 = 0;
                v1 = 0;
                while (true) {
                    label_7:
                    if (v3 >= v4) {
                        return arg11;
                    }

                    Intent v5 = arg11[v3];
                    ComponentName v2 = v5.getComponent();
                    Object v0_1 = null;
                    if (v2 != null) {
                        v2_1 = v2.getClassName();
                    } else {
                        switch (arg10) {
                            case ACTIVITY: {
                                v2_2 = arg9.getPackageManager().resolveActivity(v5, 0);
                                if (v2_2 == null) {
                                    v2_1 = null;
                                } else if (v2_2.activityInfo != null) {
                                    v2_1 = v2_2.activityInfo.name;
                                } else {
                                    v2_1 = null;
                                }

                                break;
                            }
                            case SERVICE: {
                                v2_2 = arg9.getPackageManager().resolveService(v5, 0);
                                if (v2_2 == null) {
                                    v2_1 = null;
                                } else if (v2_2.activityInfo != null) {
                                    v2_1 = v2_2.serviceInfo.name;
                                } else {
                                    v2_1 = null;
                                }

                                break;
                            }
                            default:
                                v2_1 = null;
                                break;
                        }
                    }

                    label_14:
                    v0_1 = com.bytedance.common.plugin.a.b.get(v2_1);
                    if (Logger.debug()) {
                        Logger.d("Plugin", "get proxyClass = " + v0_1 + " from pluginClass = " + v2_1);
                    }

                    if (!TextUtils.isEmpty(((CharSequence) v0_1))) {
                        v5.setClass(arg9, Class.forName(((String) v0_1)));
                        arg11[v1] = v5;
                        v0_2 = v1 + 1;
                    } else {
                        v0_2 = v1;
                        ++v3;
                        v1 = v0_2;
                        continue;
                    }

                    ++v3;
                    v1 = v0_2;
                    continue;
                }
            } catch (Throwable v0) {
                return arg11;
            }
        }

        return arg11;
    }
}

